package com.cn.serverline.lineicp.produce.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cn.serverline.lineicp.produce.entity.ProcessDescription;
import com.cn.serverline.lineicp.produce.entity.vo.ProcessDescriptionVo;
import com.cn.serverline.lineicp.produce.entity.vo.ProcessStepVo;
import com.cn.serverline.lineicp.produce.entity.vo.ResourceIdAndDeviceId;
import com.github.yulichang.base.MPJBaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 生产执行工艺描述 Mapper 接口
 * </p>
 *
 * @author 王烁
 * @since 2024-08-12
 */
@Mapper
public interface ProcessDescriptionMapper extends MPJBaseMapper<ProcessDescription> {
    @Select("SELECT\n" +
            "\t\ttsr.ID,\n" +
            "\t\ttde.ID AS deviceId,\n" +
            "\t\ttde.DEVICE_NAME,\n" +
            "    tde.DEVICE_STATE,\n" +
            "\t\ttdn.ID AS dispositionId,\n" +
            "\t\ttdn.DISPOSITION_NAME,\n" +
            "    tsr.COLLECT_VALUE,\n" +
            "    tsr.NOMINAL_VALUE,\n" +
            "    tsr.MAX_VALUE,\n" +
            "    tsr.MIN_VALUE,\n" +
            "\t\ttsr.DEVICE_DISTINGUISH\n" +
            "FROM\n" +
            "    TE_PROCESS_DESCRIPTION tpd \n" +
            "\tLEFT JOIN \n" +
            "\t\tTE_STEP_REQUIRED tsr ON tsr.PROCESS_DESCRIPTION_ID=tpd.ID\n" +
            "\tLEFT JOIN\n" +
            "\t\tTE_DISPOSITION tdn ON tdn.ID = tsr.STEPS_PARAMETER_ID\n" +
            "\tLEFT JOIN\n" +
            "\t\tTE_DEVICE tde  ON tde.ID=tsr.DEVICE_ID\n" +
            " \tWHERE\n" +
            "\t tpd.ID=#{tpdId}")
    List<ProcessStepVo> getProcessStepDetails(@Param("tpdId") Long tpdId);

    @Select("<script>" +
            "SELECT tr.ID AS resourceId," +
            "td.ID AS deviceId" +
            " FROM TE_PRO_DEVICE_TOOL_USER tpdtu " +
            "LEFT JOIN TE_PRO_MODEL tpm ON tpm.ID = tpdtu.PID " +
            "LEFT JOIN TE_RESOURCE tr ON tr.ID = tpdtu.RESOURCE_ID " +
            "LEFT JOIN TE_DEVICE td ON tr.DEVICE_ID = td.ID " +
            "WHERE tpm.ID = #{stationId} " +
            "AND td.ID IN " +
            "<foreach item='item' index='index' collection='deviceId' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    List<ResourceIdAndDeviceId> selectResource(@Param("stationId") String stationId, @Param("deviceId") List<Long> deviceId);

    void updateBatch(@Param("list") List<ProcessDescription> processDescriptions);
}
